package home.ybubnov.bugcrss.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

@NamedQuery(name="WebPage.getWebPage", query="select wp from WebPage wp")
@Entity
public class WebPage{
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Key id = null;
	
	private Text content = null;
	
	private HashSet<String> addreses = null;

	@OneToMany(targetEntity=Update.class, cascade=CascadeType.ALL)
	private LinkedList<Update> updates = null;
	
	public Key getId() {
		return id;
	}
	
	public void setId(Key id) {
		this.id = id;
	}
	
	public Text getContent() {
		return content;
	}
	
	public void setContent(Text content) {
		this.content = content;
	}
	
	public String getContentAsString() {
		if (content == null){
			return "";
		}
		return content.getValue();
	}
	
	public void setContentAsString(String content) {
		this.content = new Text(content);
	}

	public void setAddreses(HashSet<String> addreses) {
		this.addreses = addreses;
	}
	
	public HashSet<String> getAddreses() {
		if (this.addreses == null){
			return new HashSet<String>();
		}
		return addreses;
	}
	
	public void addAddreses(Set<String> addreses) {
		if (this.addreses == null){
			this.addreses = new HashSet<String>();
		}
		this.addreses.addAll(addreses);
	}

	public void removeAddreses(Set<String> addreses) {
		if (this.addreses == null){
			return;
		}
		this.addreses.removeAll(addreses);
	}
	
	public LinkedList<Update> getUpdates() {
		return updates;
	}
	
	public void setUpdates(LinkedList<Update> updates) {
		this.updates = updates;
	}
	
	public void addUpdate(Date date, String diff) {
		if (this.updates == null){
			this.updates = new LinkedList<Update>();
		}
		
		updates.add(new Update(date, diff));
	}
	
	
}
